//app.js
App({
  globalData: {
    tel:"",
    mopenid:null,
    goRe:true,
    userId:""
  },
  onLaunch: function () {
    var that = this
    //调用API从本地缓存中获取数据
    wx.login({
      success(res) {
        if (res.code) {
          console.log(res.code)
          //发起网络请求
          wx.request({
            url: 'https://thtl.vip/jishu/getOpenId',
            data: {
              code:res.code
            },
            header: {
              'content-type': 'json'
            },
            success: function (res) {
              var openid = res.data //返回openid
              
              console.log('openid为' + openid);
        
              that.globalData.mopenid = openid
              wx.request({
                url:'https://thtl.vip/jishu/doLogin',
                data:{
                  openId: openid
                },
                success:function(res){
                  console.log(res.data)
                  if(res.data==''){
                    console.log('去注册')
                    that.globalData.goRe = true;
                  }else{
                    that.globalData.userId = res.data.id;
                    console.log('不去注册')
                    that.globalData.goRe=false;
                  }
                }
              })
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        this.globalData.Custom = custom;
        this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      }
    })
  },
  onShow: function(){
    console.log('onShow');
  },
  onHide: function(){
    console.log('onHide');
  },
  getUserInfo:function(cb){
    var that = this;
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo;
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      });
    }
  },
  globalData:{
    userInfo:null
  }
})
